home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / malloc_db / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  923 b   |  52 lines

  1. /* test.c
  2.  *
  3.  * test for malloc heap trash debugger
  4.  */
  5.  
  6. #include <malloc.h>
  7.  
  8. main()
  9. {
  10.  
  11.     int          i;
  12.     char    * memory[ 100 ];
  13.  
  14. #if MDB_DEBUG
  15.     /* THIS DOESN'T NEED TO BE DONE IF YOU DO A "setenv DB_CHECK ON"  */
  16.     extern int    db_check_on;
  17.     db_check_on = 1;
  18. #endif
  19.  
  20. #if CLIBS_DEBUG
  21.     mallopt( M_DEBUG, 1 );
  22. #endif
  23.     
  24.     /* allocate 100 segments of memory                    */
  25.     for ( i = 0; i < 100; i ++ ) {
  26.     
  27.     memory[ i ] = malloc( i );
  28.  
  29.     }
  30.  
  31.     /* deallocate half of em                        */
  32.     for ( i = 0; i < 50; i++ ) {
  33.     
  34.     free( memory[ i * 2 ] );
  35.  
  36.     }
  37.     
  38.     printf( "OK so far - lets detect one ! %x !\n", ( memory[ 49 ] - 2 ) );
  39.  
  40.     * ( memory[ 49 ] - 2 ) = 0;        /* Trash the heap            */
  41.     * ( memory[ 49 ] - 10 ) = 0;    /* And again            */
  42.     
  43.     for ( i = 0; i < 50; i++ ) {
  44.  
  45.     printf( "%d\n", i );
  46.     free( memory[ 1 + i * 2 ] );
  47.  
  48.     }
  49.  
  50.     printf( "Bummer - didn't notice anything wrong !! Error !!\n" );
  51. }
  52.